package com.prj.ufdm.demo.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.elasticsearch.client.transport.TransportClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.prj.ufdm.demo.dao.atest.AtestDao;
import com.prj.ufdm.demo.dao.res.ResDwxxDao;
import com.prj.ufdm.demo.model.atest.Atest;
import com.prj.ufdm.demo.model.res.ResDwxx;
import com.prj.ufdm.demo.model.res.ResGrxx;
import com.prj.ufdm.demo.tutorial.es.ElasticMappingBuilder;
import com.prj.ufdm.demo.tutorial.es.ElasticSearchClient;

@RestController
public class HomeAction {
	
	@Autowired
	private AtestDao atestDao;
	@Autowired
	private ResDwxxDao resDwxxDao;
	
	@RequestMapping("/")
	String home() {
		
		Atest atest = new Atest();
		atest.setName("张");
		System.out.println(atestDao.selectList(atest));
		
		return "Hello World!";
	}
	
	@RequestMapping("/dwxx")
	String grxx() throws Exception {
		
		ElasticSearchClient esclient = new ElasticSearchClient();
		TransportClient transClient = esclient.createClient();
		
		List<ResDwxx> listGrxx = resDwxxDao.selectList(null);
		for(ResDwxx resDwxx : listGrxx) {
			Map<String,Object>  mapMessage = new HashMap<String,Object>();
			mapMessage.put("dwzh", resDwxx.getDwzh());
			mapMessage.put("dwmc", resDwxx.getDwmc());
			mapMessage.put("dwdz", resDwxx.getDwdz());
			mapMessage.put("dwfrdbxm", resDwxx.getDwfrdbxm());
			mapMessage.put("dwfrdbzjlx", resDwxx.getDwfrdbzjlx());
			mapMessage.put("dwfrdbzjhm", resDwxx.getDwfrdbzjhm());
			mapMessage.put("dwlsgx", resDwxx.getDwlsgx());
			mapMessage.put("dwjjlx", resDwxx.getDwjjlx());
			mapMessage.put("dwsshy", resDwxx.getDwsshy());
			mapMessage.put("dwyb", resDwxx.getDwyb());
			mapMessage.put("dwdzxx", resDwxx.getDwdzxx());
			mapMessage.put("jbrxm", resDwxx.getJbrxm());
			mapMessage.put("jbrgddhhm", resDwxx.getJbrgddhhm());
			mapMessage.put("jbrsjhm", resDwxx.getJbrsjhm());
			mapMessage.put("jbrzjlx", resDwxx.getJbrzjlx());
			mapMessage.put("jbrzjhm", resDwxx.getJbrzjhm());
			mapMessage.put("zzjgdm", resDwxx.getZzjgdm());
			mapMessage.put("dwslrq", resDwxx.getDwslrq());
			mapMessage.put("dwkhrq", resDwxx.getDwkhrq());
			mapMessage.put("dwfxr", resDwxx.getDwfxr());
			mapMessage.put("styhmc", resDwxx.getStyhmc());
			mapMessage.put("dqmc", resDwxx.getOrganName());
			
			// 创建索引
			esclient.createIndexDocement(transClient,ElasticMappingBuilder.INDEX_RES_LIB,ElasticMappingBuilder.INDEX_TYPE_DWXX,mapMessage);
		}
		System.out.println("设置成功.....");
		return "dwxx:"+listGrxx.size();
	}
	
}
